package com.pzr.soccer.model;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.data.mongodb.core.mapping.Document;
@Slf4j
@Document
@Data
public class Role {
	public static final String DBAdmin_Name = "DBAdmin";
	public static final String DBAdmin_NameZh = "超级管理员";
	public static final int DBAdmin_code = 90;

	public static final String Admin_Name = "Admin";
	public static final String Admin_NameZh = "管理员";
	public static final int Admin_code = 80;

	public static final String SuperVip_Name = "SuperVip";
	public static final String SuperVip_NameZh = "白金会员";
	public static final int SuperVip_code = 70;

	public static final String Vip_Name = "Vip";
	public static final String Vip_NameZh = "黄金会员";
	public static final int Vip_code = 60;

	public static final String Normal_Name = "Normal";
	public static final String Normal_NameZh = "普通会员";
	public static final int Normal_code = 50;

	public static final String Guest_Name = "Guest";
	public static final String Guest_NameZh = "游客";
	public static final int Guest_code = 40;

	private String id;
	private String name;
	private String nameZh;
	private LocalDate expiredDate;

	// 各个客户端都有自己一套权限， 用户登录时必须携带client标识来加载权限
	private String client;
	private int code;
	private boolean isExpired;

	public boolean isExpired() {
		LocalDate today = LocalDate.now();
		return !today.isBefore(this.expiredDate);
	}

	private Role(String name, String nameZh, int code, LocalDate expiredDate) {
		this.name = name;
		this.nameZh = nameZh;
		this.code = code;
		this.expiredDate = expiredDate;
	}

	public static Role DBAdminRole(LocalDate expiredDate) {
		return new Role(DBAdmin_Name, DBAdmin_NameZh, DBAdmin_code, expiredDate);
	}

	public static Role AdminRole(LocalDate expiredDate) {
		return new Role(Admin_Name, Admin_NameZh, Admin_code, expiredDate);
	}

	public static Role SuperVipRole(LocalDate expiredDate) {
		return new Role(SuperVip_Name, SuperVip_NameZh, SuperVip_code, expiredDate);
	}

	public static Role VipRole(LocalDate expiredDate) {
		return new Role(Vip_Name, Vip_NameZh, Vip_code, expiredDate);
	}

	public static Role NormalRole(LocalDate expiredDate) {
		return new Role(Normal_Name, Normal_NameZh, Normal_code, expiredDate);
	}

	public static Role GuestRole(LocalDate expiredDate) {
		return new Role(Guest_Name, Guest_NameZh, Guest_code, expiredDate);
	}

	public static int getTopCode(Collection<Role> roles) {
		int code = 0;
		for (Role r : roles) {
			if (r.getCode() > code) {
				code = r.getCode();
			}
		}
		return code;
	}

	public static Role getTop(Collection<Role> roles) {
		int code = getTopCode(roles);
		for (Role r : roles) {
			if (r.getCode() == code) {
				return r;
			}
		}
		return null;
	}

	public static void sortRoleDesc(List<Role> roles) {
		Collections.sort(roles, new Comparator<Role>() {
			@Override
			public int compare(Role o1, Role o2) {
				return o1.getCode() - o2.getCode();
			}
		});
	}

	public static void sortRoleAscen(List<Role> roles) {
		Collections.sort(roles, new Comparator<Role>() {
			@Override
			public int compare(Role o1, Role o2) {
				return o2.getCode() - o1.getCode();
			}
		});
	}

	public static Role build(String roleName,LocalDate expiredDate) {
		switch(roleName) {
		case Role.Admin_Name : return Role.AdminRole(expiredDate) ;
		case Role.DBAdmin_Name : return Role.DBAdminRole(expiredDate) ;
		case Role.SuperVip_Name : return Role.SuperVipRole(expiredDate) ;
		case Role.Vip_Name : return Role.VipRole(expiredDate);
		case Role.Normal_Name : return Role.NormalRole(expiredDate) ;
		case Role.Guest_Name : return Role.GuestRole(expiredDate) ;

		default: log.info("no such role name,build faild!");return null;
		}
	}
}
